package cn.csxw.openchina.ui.fragment.syntheticalFragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.Toast;

import java.util.ArrayList;

import javax.inject.Inject;

import cn.csxw.openchina.base.BaseRecAdapter;
import cn.csxw.openchina.base.BaseRefreshFragment;
import cn.csxw.openchina.dagger2.component.DaggerNewsComponent;
import cn.csxw.openchina.dagger2.module.NewsModule;
import cn.csxw.openchina.dagger2.presenter.NewsPresenter;
import cn.csxw.openchina.entity.bean.NewsBean;
import cn.csxw.openchina.ui.adapter.InformationAdapter;
import cn.csxw.openchina.utils.Global;

/**
 *  综合Fragment下的  资讯Fragment
 */
public class InformationFragment extends BaseRefreshFragment {
    private int pageIndex = 0;
    @Inject
    public NewsPresenter mNewsPresenter;
    public InformationAdapter mInformationAdapter;
    @Override
    protected BaseRecAdapter getAdapter() {
        //以下代码仅仅是测试用
        ArrayList<NewsBean>informationData = new ArrayList<>();
        if(mInformationAdapter==null){
            mInformationAdapter = new InformationAdapter(informationData);
        }
        return mInformationAdapter;
    }

    @Override
    public void doRefresh() {
        mInformationAdapter.getData().clear();
        mNewsPresenter.callNetWork(0, 1, 20);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setOnRefreshListener(mOnRefreshListener);
        DaggerNewsComponent.builder().newsModule(new NewsModule(this)).build().in(this);
        Global.onNetworkStateListener(mNewsPresenter,this);
        //TODO: CallNetWork中记得根据常数类型个数传Object对象 如:
        mNewsPresenter.callNetWork(0, 1, 20);
    }
    BaseRefreshFragment.OnRefreshListener mOnRefreshListener = new OnRefreshListener() {
        @Override
        public void onUpLoadMore() {
            if (Global.isNetworkReachable(getActivity())) {
                pageIndex++;
                mNewsPresenter.callNetWork(pageIndex,4,20);
            }else {
                Toast.makeText(getActivity(), "网络连接异常,无法加载更多",Toast.LENGTH_SHORT).show();
            }
        }
    };
}
